package com.ejd.web.vo.goods.detail;

import java.awt.Dimension;

import java.io.IOException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.faces.application.FacesMessage;
import javax.faces.application.ViewHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.cglib.beans.BeanCopier;

import com.ejd.common.config.ConfigManager;
import com.ejd.controller.LoginController;
import com.ejd.service.ICodeMasterService;
import com.ejd.service.IColorService;
import com.ejd.service.IConsultationService;
import com.ejd.service.ICustomerService;
import com.ejd.service.IGoodsService;
import com.ejd.utils.DESPlusUtil;
import com.ejd.utils.DateTimeUtil;
import com.ejd.utils.MessageFactory;
import com.ejd.utils.SendHtmlMail;
import com.ejd.utils.SortManagerDelegate;
import com.ejd.utils.SpringFacesUtil;
import com.ejd.utils.WebPageData;
import com.ejd.web.bo.impl.CodeMaster;
import com.ejd.web.bo.impl.Color;
import com.ejd.web.bo.impl.Consultation;
import com.ejd.web.bo.impl.Customer;
import com.ejd.web.bo.impl.Goods;
import com.ejd.web.bo.impl.GoodsProperties;
import com.ejd.web.constant.EjdConstants;
import com.ejd.web.constant.ManageBeanConstants;
import com.ejd.web.constant.SpringBeanConstants;
import com.ejd.web.vo.common.ColorVo;
import com.ejd.web.vo.common.ConsultationPage;
import com.ejd.web.vo.common.ConsultationVo;
import com.ejd.web.vo.common.CustomerVo;
import com.ejd.web.vo.common.GoodsPropertiesVo;
import com.ejd.web.vo.common.GoodsVo;
import com.ejd.web.vo.customer.LoginCustomerPageBean;
import com.ejd.web.vo.genl.GenerateVerifyCodeBean;
@ManagedBean(name="goodsImagePage")
@ViewScoped

public class GoodsImagePageBean implements java.io.Serializable {
	private static final long serialVersionUID = -8106629465344224719L;
	private String code;
	private GoodsVo goods;
	private String specialField;//for selected color
	private String sessionId;
	private Integer length;

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
	public GoodsVo getGoods() {
		return goods;
	}

	public void setGoods(GoodsVo goods) {
		this.goods = goods;
	}

	public String getSpecialField() {
		return specialField;
	}

	public void setSpecialField(String specialField) {
		this.specialField = specialField;
	}

	public String getSessionId() {
		return sessionId;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}
	
	public Integer getLength() {
		return length;
	}

	public void setLength(Integer length) {
		this.length = length;
	}

	public GoodsImagePageBean() throws IOException {
		String code = (String) SpringFacesUtil.getRequestParameter("code");
		code = DESPlusUtil.decrypt(code);
		String sessionId = (String) SpringFacesUtil.getRequestParameter("sessionId");
		sessionId = DESPlusUtil.decrypt(sessionId);
		String currentSessionId = SpringFacesUtil.getCurrentSessionId();
		if (null == sessionId || "".equals(sessionId) || null == currentSessionId || "".equals(currentSessionId) || !(sessionId.equals(currentSessionId))) {
			SpringFacesUtil.redirectPage(EjdConstants.INDEX_HTML);
		}
		this.setSessionId(sessionId);
		String specialField =(String) SpringFacesUtil.getRequestParameter("specialField");
		if (null != specialField && !("".equals(specialField))) {
			specialField = DESPlusUtil.decrypt(specialField);
		}
		this.setSpecialField(specialField);
		if (null == code || "".equals(code) || "null".equals(code)) {
			SpringFacesUtil.redirectPage(EjdConstants.INDEX_HTML);
		} else {
			this.setCode(code);
			IGoodsService<Goods,Long> goodsService = (IGoodsService<Goods,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.GOODS_SERVICE_BEAN_NAME);
			goodsService.setEntityClass(Goods.class);
			goodsService.setPKClass(Long.class);
			Goods currGoods = goodsService.findUniqueBy("code", code);
			BeanCopier copyGoods = BeanCopier.create(Goods.class, GoodsVo.class, false);
			GoodsVo goodsVo = new GoodsVo();
			copyGoods.copy(currGoods, goodsVo, null);
			Set<GoodsProperties> currGoodsProperties = currGoods.getProperties();
			List<GoodsPropertiesVo> pgoodsProperties =new ArrayList<GoodsPropertiesVo>();
			if (null !=currGoodsProperties && !(currGoodsProperties.isEmpty())) {
				Iterator<GoodsProperties> iterator = currGoodsProperties.iterator();
				while(iterator.hasNext()) {
					GoodsProperties tempGoodsProperties = iterator.next();
					GoodsPropertiesVo tempGoodsPropertiesVo = new GoodsPropertiesVo();
					BeanCopier copyHere = BeanCopier.create(GoodsProperties.class,GoodsPropertiesVo.class,false);
					copyHere.copy(tempGoodsProperties, tempGoodsPropertiesVo,null);
					pgoodsProperties.add(tempGoodsPropertiesVo);
				}
			}
			goodsVo.setPropertiesv(pgoodsProperties);
			this.setGoods(goodsVo);
		
		}
	}
	
	public List<String> getImagesList(){
		List<String> result = null;
		if (null == this.getGoods().getVoPropertiesMapShowImages() || this.getGoods().getVoPropertiesMapShowImages().isEmpty()) {//从主数据中拿数据
			result = this.getGoods().getVoImages();
		} else {
			if (null == this.getSpecialField() || "".equals(this.getSpecialField())) {
				Collection<List<String>> it =  this.getGoods().getVoPropertiesMapImages().values();
				result = it.iterator().next();	
			} else {
				result = this.getGoods().getVoPropertiesMapImages().get(this.getGoods().getCode()+this.getSpecialField());
			}
		}
		Integer length = 0;
		for (String s:result) {
			if (null != s && !("".equals(s)) && !("null".equals(s))) {
				length = length +1;
			}
		}
		this.setLength(length);
		return result;
	}

}
